<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of template_controller
 *
 * @author marcelo.diaz
 */
class Template_controller extends Controller{

  public $controler_name;
  public $model_name;
  public $js_find_function;

  function Template_controller($controller, $model, $js_funct){
    parent::Controller();
    $this->controler_name = $controller;
    $this->model_name = $model;
    $this->js_find_function = $js_funct;
    $this->load->model($model_name);
  }

	function index()
	{
      $email = "ALL";
      $this->email->from('conocepunta@gmail.com');
      $this->email->to('conocepunta@gmail.com');
      $this->email->subject($controler_name.' access');
      $data['page_title'] = $controler_name." en Punta Alta";
      $output = $this->load->view('head_view', $data, true);
      $places = $this->model_name->_all();
      //iterate and parse places
      $xml = new SimpleXMLElement($places);
      $content = "<div class='content centered'><div class='lheader' style='width:89%'>";
      foreach ($xml->marker as $place){
        $content .= "<div id='".$place['name']."' class='menu item' onclick='".$this->js_find_function."(this.id);'>".
                    "<img class='lheader' src='".base_url().$place->icon."'>".
                    "<div class='title'><p>".str_replace("-", " ", $place['name'])."</p><p class='comment'>".$place->add."</p>".
                    "<p><a class='comment' href='".base_url().$this->controler_name."/nombre/".$place['name']."'>Mas informacion...</a></p></div>".
                   "</div>";
      }
      $content .= "</div>";

      $banner_adsense = '<div class="lheader"><script type="text/javascript"><!--
                          google_ad_client = "pub-2113662635661393";
                          /* 120x600, creado 21/12/10 */
                          google_ad_slot = "3994223914";
                          google_ad_width = 120;
                          google_ad_height = 600;
                          //-->
                          </script>
                          <script type="text/javascript"
                          src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
                          </script></div>';

      $output .= "<div class='content centered'>";
      $output .= $this->load->view('map_fb_view', "", true);
      $output .= "</div>";
      $output .= $content.$banner_adsense."</div>";
      $output .= $this->load->view('foot_view', "", true);
      $this->output->set_output($output);
      $this->email->message($email);
      $this->email->send();
    }

    function name($place){
      return $this->model_name->get_place($place);
    }

    function all(){
      return $this->model_name->get_all();
    }

    function nombre($name = false){
      if ($name){
        $email = "[".$name."]";
        $this->email->from('conocepunta@gmail.com');
        $this->email->to('conocepunta@gmail.com');
        $this->email->subject($this->controler_name.' access');
        $data['page_title'] = $this->controler_name." en Punta Alta";
        $output = $this->load->view('head_view', $data, true);
        $output .= "<div class='content centered' style='height:120px'>";
        $content = "";
        $place = $this->model_name->_place($name);
        if (!$place){
          $output .= "<div class='ui-state-error'><p><strong>".str_replace("-", " ", $name)."</strong> NO se encuentra en el sitio.</p>".
                     "<p>Contactate por <strong><a href='http://www.facebook.com/pages/Conoce-Punta/145094618228' target='_new'>Facebook</a></strong> o por <strong><a href='http://www.twitter.com/conocepunta' target='_new'>Twitter</a></strong> y lo agregamos.</p></div>";

          $email .= "NOT FOUND";
        } else{
          //parse place
          $xml = new SimpleXMLElement($place);
          $image = ($xml->img == "") ? "<img class='lheader' src='".$xml->icon."'>" : "<img class='post_img avatar lheader' src='".$xml->img."'>";
          $desc = "";
          foreach ($xml->desc as $d){
            $desc .= "<p class='comment'>".$d."</p>";
          }
          $content = "<div id='".$xml['name']."' class='lheader map_info'  onclick='".$this->js_find_function."(this.id);'>".
                        "<div class='option'>".$image."<p>".str_replace("-", " ", $xml['name'])."</p><p class='comment'>".$xml->add."</p>".$desc."</div>".
                        "<div class='fb_like'><iframe src='http://www.facebook.com/plugins/like.php?href=www.conocepunta.com.ar/".$this->controler_name."/nombre/".$xml['name']."' layout='button_count' action='like' scrolling='no' frameborder='0' allowTransparency='true'></iframe></div>".
                      "</div>";
          $banner_adsense = '<div class="lheader"><script type="text/javascript"><!--
                              google_ad_client = "pub-2113662635661393";
                              /* 728x90, creado 16/02/10 */
                              google_ad_slot = "4418063429";
                              google_ad_width = 728;
                              google_ad_height = 90;
                              //-->
                              </script>
                              <script type="text/javascript"
                              src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
                              </script></div>';
          $output .= $banner_adsense;

          $email.= $xml->asXML();
        }
        $output .= "</div>";
        $output .= "<div class='content centered'>";
        $output .= $content;
        $output .= $this->load->view('map_fb_view', "", true);
        $output .= "</div>";
        $output .= $this->load->view('foot_view', "", true);
        $this->output->set_output($output);
        $this->email->message($email);
        $this->email->send();
      }else{
        $this->index();
      }
    }
}
?>
